﻿using MES.Desktop.BasicDataModule.Models;
using MES.Desktop.Common;
using MES.Desktop.IService;
using Prism.Events;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MES.Desktop.BasicDataModule.ViewModels
{
    public class MaterialClassViewModel : PageViewModelBase
    {
        public ObservableCollection<MaterialClassModel> MaterClasses { get; set; } =
            new ObservableCollection<MaterialClassModel>();

        List<Entities.MaterialClassEntity> origMaterClass;

        private bool isSyncSelect;
        private bool _isSelectAll;

        public bool IsSelectAll
        {
            get { return _isSelectAll; }
            set
            {
                SetProperty<bool>(ref _isSelectAll, value);

                if (isSyncSelect) return;
                isSyncSelect = true;

                foreach (var mc in MaterClasses)
                    mc.IsSelected = value;

                isSyncSelect = false;
            }
        }



        IMaterialService _materialService;
        IEventAggregator _eventAggregator;

        public MaterialClassViewModel(
            IRegionManager regionManager,
            IMaterialService materialService,
            IEventAggregator eventAggregator)
            : base(regionManager)
        {
            this.PageTitle = "物料类别管理";

            _materialService = materialService;
            _eventAggregator = eventAggregator;

            this.Refresh();
        }

        public override void Refresh()
        {
            MaterClasses.Clear();

            _eventAggregator.GetEvent<LoadingEvent>()
                .Publish("正在加载物料分类....");


            Task.Run(() =>
            {
                try
                {
                    origMaterClass = _materialService.GetClassAll(SearchKey).ToList();
                    this.GenerateMCTree(MaterClasses, null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示");
                }
                finally
                {
                    _eventAggregator.GetEvent<LoadingEvent>()
                        .Publish("");
                }
            });
        }

        private void GenerateMCTree(
            ObservableCollection<MaterialClassModel> m_class,
            MaterialClassModel parent,
            bool expandedNode = true)
        {
            var sub = origMaterClass.Where(m => m.ItemTypeId != 0 &&
                    m.ParentTypeId == (parent == null ? 0 : parent.ItemTypeId)).ToList();

            if (sub.Count() > 0)
            {
                foreach (Entities.MaterialClassEntity item in sub)
                {
                    MaterialClassModel model = new MaterialClassModel
                    {
                        ItemTypeId = item.ItemTypeId,
                        ItemTypeCode = item.ItemTypeCode,
                        ItemTypeName = item.ItemTypeName,
                        ParentTypeId = item.ParentTypeId,
                        Ancestors = item.Ancestors,
                        ItemOrProduct = item.ItemOrProduct,
                        OrderNum = item.OrderNum,
                        EnableFlag = item.EnableFlag,
                        IsExpanded = expandedNode,
                        Parent = parent,
                        Remark = item.Remark,
                        attr1 = item.attr1,
                        attr2 = item.attr2,
                        attr3 = item.attr3,
                        attr4 = item.attr4,
                        CreateBy = item.CreateBy,
                        CreateTime = item.CreateTime,
                        UpdateBy = item.UpdateBy,
                        UpdateTime = item.UpdateTime,
                    };

                    model.PropertyChanged += (se, ev) =>
                    {
                        if (isSyncSelect) return;
                        isSyncSelect = true;

                        if (ev.PropertyName == "IsSelected")
                            this.IsSelectAll = MaterClasses.All(f => f.IsSelected);

                        isSyncSelect = false;
                    };
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        m_class.Add(model);
                    });

                    GenerateMCTree(model.Children, model);
                }

                if (m_class.Count > 0)
                {
                    m_class[m_class.Count - 1].IsLastChild = true;
                }
            }
        }
    }
}
